#!/bin/bash
echo ""
echo "===== SYSTEM OVERVIEW ====="
echo "OS:     |$(lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -1)"
echo "Kernel: |$(uname -r)"
echo "Arch:   |$(uname -m)"
echo "Uptime: |$(uptime -p | sed 's/up //')"

echo ""
echo "===== RESOURCE USAGE ====="
free -m | awk '/Mem/{printf "Memory: |%sMi/%sMi (%.1f%%)\n", $3,$2,$3/$2 * 100}'
df -h / | awk '/\//{printf "Disk:   |%s/%s (%s)\n", $3,$2,$5}'

# IP地址处理（完美兼容所有系统）
ipv4=$( { command -v ip >/dev/null && ip -4 addr || ifconfig; } 2>/dev/null | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v 127.0 | head -1)
ipv6=$( { command -v ip >/dev/null && ip -6 addr || ifconfig; } 2>/dev/null | grep -oP '(?<=inet6\s)[\da-f:]+' | grep -v ::1 | head -1)
echo "IP:     |${ipv4:-N/A} ${ipv6:-N/A}"

echo ""
echo "===== HARDWARE INFO ====="
cpu_model=$(lscpu 2>/dev/null | awk -F': +' '/Model name/{print $2}' | sed 's/(R)//g; s/@.*//')
[ -z "$cpu_model" ] && cpu_model=$(cat /proc/cpuinfo | awk -F': ' '/model name/{print $2; exit}')
echo "CPU:    |${cpu_model:-Unknown}"

# 温度检测终极解决方案（树莓派专用处理）
get_temp() {
    # 树莓派优先处理（彻底解决'C°C问题）
    if [ -f /usr/bin/vcgencmd ]; then
        temp=$(vcgencmd measure_temp 2>/dev/null | awk -F= '{gsub(/\047C/, "", $2); print $2}')
    else
        # 标准传感器
        temp=$(sensors 2>/dev/null | awk '/^temp|Core/{gsub(/[^0-9.]/, "", $2); print $2}' | head -1)
        [ -z "$temp" ] && temp=$(cat /sys/class/thermal/thermal_zone*/temp 2>/dev/null | awk '{printf "%.1f", $1/1000}' | head -1)
    fi
    # 最终标准化输出
    [ -n "$temp" ] && echo "${temp}°C" || echo "N/A"
}
echo "Temp:   |$(get_temp)"

# 用户统计（精确统计）
echo "Users:  |$(who | awk '!/^root/{count++} END{print count}')"

echo ""
echo "===== PROCESSES ====="
ps -eo pid,user:8,%cpu,%mem,comm --sort=-%cpu 2>/dev/null | head -n 6 | awk '
    BEGIN {printf "%-6s %-8s %5s %4s %s\n", "PID", "USER", "%CPU", "%MEM", "CMD"} 
    NR>1 {cmd=$5; if(length($5)>15) cmd=substr($5,1,14)"…"; printf "%-6d %-8s %5.1f %4.1f %s\n", $1,$2,$3,$4,cmd}'
echo ""
